package com.insta.playdroid;

import java.io.File;
import java.util.List;

import adapteurMediaStore.ISongUtils;
import adapteurMediaStore.SongUtils;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

import com.insta.adapterList.SongAdapter;
import com.insta.playdroid.metier.domain.Song;

public class AlbumSongsActivity extends MediaPlayerNewListActivity {

	public static List<Song> songs;
	private ISongUtils songUtils;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.allsong);

		ImageView img = (ImageView) findViewById(R.id.imagealbumonglet);
		TextView txAlbum = (TextView) findViewById(R.id.nomalbum);
		TextView txArtist = (TextView) findViewById(R.id.nomchanteur);

		img.setVisibility(1);
		txAlbum.setVisibility(1);
		txArtist.setVisibility(1);

		// récup l'instance du player
		player = PlayerFactory.create(0, null, this);

		int position = getIntent().getExtras().getInt("album");

		String imageart = getIntent().getExtras().getString("image");
		if (imageart != null)
			img.setImageURI(Uri.fromFile(new File(imageart)));
		else
			img.setBackgroundResource(R.drawable.lecteur_piano);

		int largeurPixel = getResources().getDisplayMetrics().widthPixels;
		img.setAdjustViewBounds(true);
		img.setLayoutParams(new LayoutParams(largeurPixel / 2, largeurPixel / 2));

		// charger les song de l'album choisi
		songUtils = new SongUtils(getBaseContext());
		songs = songUtils.getSongAlbum(position);

		txAlbum.setText(songs.get(0).getAlbum());
		txArtist.setText(songs.get(0).getArtist());

		// charger les items albums
		ListView lv = (ListView) findViewById(android.R.id.list);
		lv.setTextFilterEnabled(true);

		SongAdapter adapter = new SongAdapter(this, songs);
		lv.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		// update le player
		player.setSongs(songs);
		player.setSong(position);
		player.setContext(this);
		// démarer player
		player.startPlayerActivity();
	}
}
